//
//  ViewController.m
//  AdvertisementI
//
//  Created by mac on 16/5/19.
//  Copyright © 2016年 Apple inc. All rights reserved.
//

#import "ViewController.h"
#import "loginViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor redColor];
    [self changeViewContoller];
}
-(void)changeViewContoller{
    
    //判断是否需要登录,如果需要登录跳转到登录界面
    if ([self isNeedLogin]) {
        //需要登录,跳登录界面
        loginViewController *loginVC = [[loginViewController alloc] init];
        _loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self.view addSubview:_loginNav.view];
        
    }
    else
    {
        //不需要登录,跳主界面
        [self goToMainVC];
    }
}
//判断是否需要登录
-(BOOL)isNeedLogin{
//    判断本地用户名是否存在
    NSString *token = [APP_DEF objectForKey:TOKEN_KEY];    if (token) {
        
        NSDate *endTime = [APP_DEF objectForKey:TIME_KEY];
        NSDate *now = [NSDate date];
        NSComparisonResult comparisonResult =[now compare:endTime];
        
        if (comparisonResult)         {
            //没过期
            return NO;
        }
        return YES;
    }
    return YES;
}

-(void)goToMainVC{
    //设置导航栏颜色
    [UINavigationBar appearance].barTintColor=BuleColor;
    
    _technicalVC = [[TechnicalViewController alloc]init];
    _solutionVC = [[SolutionViewController alloc]init];
    _friendVC = [[FriendsViewController alloc]init];
    _partsVC = [[PartsViewController alloc]init];
    _meVC = [[MeViewController alloc]init];
    
    _technicalVC.title=@"Chips";//主页
    _solutionVC.title=@"Friends";//
    _friendVC.title=@"Solution";
    _partsVC.title=@"IOT";
    _meVC.title=@"Me";
    

//    默认状态和选中状态合并
    _technicalVC.tabBarItem.image = [UIImage imageNamed:@"红色要闻"];
    _solutionVC.tabBarItem.image = [UIImage imageNamed:@"灰色友情链接"];
    _friendVC.tabBarItem.image = [UIImage imageNamed:@"红色动态"];
    _partsVC.tabBarItem.image = [UIImage imageNamed:@"红色公益"];
    _meVC.tabBarItem.image = [UIImage imageNamed:@"红色文明办"];
    
    //导航状态
    UINavigationController*firstNav=[[UINavigationController alloc]initWithRootViewController:_technicalVC];
    UINavigationController*secondNav=[[UINavigationController alloc]initWithRootViewController:_solutionVC];
    UINavigationController*thirdNav=[[UINavigationController alloc]initWithRootViewController:_friendVC];
    UINavigationController*fourthNav=[[UINavigationController alloc]initWithRootViewController:_partsVC];
    UINavigationController*fifthNav=[[UINavigationController alloc]initWithRootViewController:_meVC];
    //改变导航条的颜色
    //        [firstNav.navigationBar setBackgroundImage:[UIImage imageNamed:@"tab_sel"] forBarMetrics:UIBarMetricsDefault];
    firstNav.tabBarItem.title = @"";
    secondNav.tabBarItem.title = @"";
    thirdNav.tabBarItem.title = @"";
    fourthNav.tabBarItem.title = @"";
    fifthNav.tabBarItem.title = @"";
    
    
//    设置子视图控制器
    UITabBarController*tabBar=[[UITabBarController alloc]init];
    tabBar.viewControllers=@[firstNav,secondNav,thirdNav,fourthNav,fifthNav];
//    设置window的跟视图控制器
    UIWindow*window=[UIApplication sharedApplication].delegate.window;
    window.rootViewController=tabBar;


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
